<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>=$!{i18n.get("system")}$!{i18n.get("mng")}</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  #parse('/headFiles.html')
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">国家名称</label>
            <div class="layui-input-inline">
              <input type="text" name="name" placeholder="$!{i18n.get('pleaseEn')}" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="LAY-list-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn LAY-btn-fun" data-type="add">新增国家信息</button>
        </div>
        <table id="LAY-list" lay-filter="LAY-list"></table> 
        <script type="text/html" id="table-operate">
          	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update">修改</a>
			<a class="layui-btn layui-btn-xs" lay-event="area">地区</a>
          	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
      </div>
    </div>
  </div>

#parse('/footFiles.html')
<script type="text/javascript">
  layui.config({
    base: '$!{webRoot}/assets/layuiadmin/'
  }).extend({
    index: 'lib/index'
  }).use(['index', 'form', 'table'], function(){
    var $ = layui.$,table = layui.table,form = layui.form;
    
    table.render({
      elem: '#LAY-list'
      ,url: '$!{webRoot}/config/country/list.data'
      ,parseData: function(res){
   	    return {
   	      "code": res.code,
   	      "msg": res.msg,
   	      "count": res.body.total,
   	      "data": res.body.data
   	    };
   	  }
      ,response: {statusCode: 200}
      ,cols: [[
          {field:'id',title:'ID'}
          ,{field:'name', title: '国家名称'}
          ,{field:'fullName', title: '国家全称'}
          ,{field:'englishName', title: '国家英文'}
          ,{field:'lang', title: 'WEB字符集'}
          ,{title: '操作',toolbar: '#table-operate',width:180}
	    ]]
      ,page: true
      ,limit: 20
      ,limits: [10, 15, 20, 25, 30]
    });
    
  	//监听搜索
    form.on('submit(LAY-list-search)', function(data){
      var field = data.field;
      //执行重载
      table.reload('LAY-list', {
        where: field
        ,page: {curr:1}
      });
    });

    var active = {
    	add : function(){
    	  layer.open({
   	          type: 2
   	          ,title: '新增国家信息'
   	          ,content: "$!{webRoot}/config/country/add.html"
   	          ,maxmin: false
   	          ,area: ['550px', '500px']
   	          ,btn: ['确定','取消']
   	          ,yes: function(index, layero){
   	        	var iframeWindow = window['layui-layer-iframe'+ index]
	               ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
	               iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
	                 var field = data.field; //获取提交的字段
	               	$.ajax({
			    		type: "post",
			            url: "$!{webRoot}/config/country/add.do",
			            data:field,
						dataType:"json",
						success:function(res){
							if(res.code == 200){
								table.reload('LAY-list'); //数据刷新
		                    	layer.close(index); //关闭弹层
							}else{
								layer.msg(res.msg,{icon: 2});
							}
						}
			        });
	              });  
	              submit.trigger('click');
   	          }
   	        });
    	}
    	,update:function(obj){
    		layer.open({
 	           type: 2
 	           ,title: '修改国家信息'
 	           ,content: '$!{webRoot}/config/country/update.html?id='+obj.data.id
 	           ,maxmin: false
 	           ,area: ['550px', '500px']
               ,btn: ['确定','取消']
 	           ,yes: function(index, layero){
 	         	  var iframeWindow = window['layui-layer-iframe'+ index]
 	               ,submit = layero.find('iframe').contents().find("#LAY-form-update");
 	               iframeWindow.layui.form.on('submit(LAY-form-update)', function(data){
 	                 var field = data.field; //获取提交的字段
 	               	$.ajax({
 			    		type: "post",
 			            url: "$!{webRoot}/config/country/update.do",
 			            data:field,
 						dataType:"json",
 						success:function(res){
 							if(res.code == 200){
 								table.reload('LAY-list'); //数据刷新
 		                    	layer.close(index); //关闭弹层
 							}else{
 								layer.msg(res.msg,{icon: 2});
 							}
 						}
 			        });
 	              });  
 	              submit.trigger('click');
 	           }
 	         });
    	}
    	,del:function(obj){
    		layer.confirm('确定要删除该国家信息吗？', function(index){
            	$.ajax({
              	  type: "post",
                  url: "$!{webRoot}/config/country/del.do",
                  data:{id:obj.data.id},
          		  dataType:"json",
          		  success:function(res){
          			if(res.code == 200){
          				table.reload('LAY-list');
          			}else{
          				layer.msg(res.msg,{icon: 5});
          			}
          		  }
                });
            	layer.close(index);
            });
    	}
    	,area:function(obj){
    		parent.layui.index.openTabsPage('$!{webRoot}/config/country/area/list.html?countryId='+obj.data.id,  obj.data.name);
    	}
    }; 

    $('.layui-btn.LAY-btn-fun').on('click', function(){
    	let type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    
    table.on('tool(LAY-list)', function(obj){
      	let type = obj.event ;
      	active[type] ? active[type].call(this,obj) : '';
    });
  });
</script>
</body>
</html>
